package com.ysd.lis.service.samsto.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.mapper.samsto.SamstoFridgeModelMapper;
import com.ysd.lis.entity.samsto.SamstoFridgeModel;
import com.ysd.lis.request.samsto.SamstoFridgeModelListParam;
import com.ysd.lis.request.samsto.SamstoFridgeModelParam;
import com.ysd.lis.service.samsto.SamstoFridgeModelService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 冰箱型号(SamstoFridgeModel)表服务实现类
 *
 * @author leiming
 * @since 2024-10-24 10:23:31
 */
@Service("samstoFridgeModelService")
public class SamstoFridgeModelServiceImpl extends ServiceImpl<SamstoFridgeModelMapper, SamstoFridgeModel> implements SamstoFridgeModelService {

    private LambdaQueryWrapper<SamstoFridgeModel> initWrapper(){
        LambdaQueryWrapper<SamstoFridgeModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return lambdaQueryWrapper;
    }

    @Override
    public Result findPageList(SamstoFridgeModelParam pageParam) {
        Page<SamstoFridgeModel> pageList=null;
        LambdaQueryWrapper<SamstoFridgeModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoFridgeModel> page = new Page<SamstoFridgeModel>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.eq(SamstoFridgeModel::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SamstoFridgeModel::getBoxDoor, pageParam.getKeywords())
                    .or().like(SamstoFridgeModel::getModelName, pageParam.getKeywords())
                    .or().like(SamstoFridgeModel::getBrandName, pageParam.getKeywords()));
        }
        lambdaQueryWrapper.orderByAsc(SamstoFridgeModel::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoFridgeModel::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result findListByGId(SamstoFridgeModelParam pageParam) {
        Page<SamstoFridgeModel> pageList=null;
        LambdaQueryWrapper<SamstoFridgeModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoFridgeModel> page = new Page<SamstoFridgeModel>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        if(StringUtils.isNotBlank(pageParam.getDeptName())){
            lambdaQueryWrapper.eq(SamstoFridgeModel::getDeptName, pageParam.getDeptName());
        }
        lambdaQueryWrapper.eq(SamstoFridgeModel::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SamstoFridgeModel::getBoxDoor, pageParam.getKeywords())
                    .or().like(SamstoFridgeModel::getModelName, pageParam.getKeywords())
                    .or().like(SamstoFridgeModel::getBrandName, pageParam.getKeywords()));
        }
        lambdaQueryWrapper.orderByAsc(SamstoFridgeModel::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoFridgeModel::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result getInfoById(String id) {
        SamstoFridgeModel samstoFridgeModel=this.baseMapper.selectById(id);
        return Result.succ(1, "获取信息成功", samstoFridgeModel);
    }

    @Override
    public Result add(SamstoFridgeModelParam param) {
        SamstoFridgeModel info=new SamstoFridgeModel();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setCreator(sysUser.getId());
                param.setOrgId(sysUser.getOrgId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setCreateTime(new Date());
        BeanUtil.copyProperties(param,info);
        info.setDelFlag(0);
        info.setStatus(1);
        this.baseMapper.insert(info);
        return Result.succ(1, "添加信息成功", null);
    }

    @Override
    public Result edit(SamstoFridgeModelParam param) {
        Map returnMap = new HashMap();
        SamstoFridgeModel info=new SamstoFridgeModel();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setEditor(sysUser.getId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setEditTime(new Date());
        BeanUtil.copyProperties(param,info);
        this.baseMapper.updateById(info);
        returnMap.put("id", param.getId());
        return Result.succ(1, "修改信息成功", returnMap);
    }

    @Override
    public Result delete(List<String> ids) {
        Result result;
        try {
            boolean b = this.removeByIds(ids);
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result updelete(List<String> ids) {
        Result result;
        LambdaUpdateWrapper<SamstoFridgeModel> lambdaUpdateWrapper = new UpdateWrapper<SamstoFridgeModel>().lambda();
        // set 表示要更新的字段， eq 表示  WHERE 条件
        lambdaUpdateWrapper.set(SamstoFridgeModel::getDelFlag,1)
                .in(SamstoFridgeModel::getId, ids);
        this.baseMapper.update(new SamstoFridgeModel(), lambdaUpdateWrapper);

        try {
            int b= this.baseMapper.update(new SamstoFridgeModel(), lambdaUpdateWrapper);
            if (b>0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Transactional
    @Override
    public Result saveOrUpdateList(SamstoFridgeModelListParam param) {
        Result result;
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
               return Result.fail(400, "保存失败", "");
            }
            for (SamstoFridgeModel samstoFridgeModel : param.getInfoList()) {
                if(StringUtils.isNotBlank(samstoFridgeModel.getId())){
                    samstoFridgeModel.setEditor(sysUser.getId());
                    samstoFridgeModel.setEditTime(new Date());
                    this.baseMapper.updateById(samstoFridgeModel);
                }else{
                    samstoFridgeModel.setCreator(sysUser.getId());
                    samstoFridgeModel.setOrgId(sysUser.getOrgId());
                    samstoFridgeModel.setCreateTime(new Date());
                    samstoFridgeModel.setDelFlag(0);
                    samstoFridgeModel.setStatus(1);
                    this.baseMapper.insert(samstoFridgeModel);
                }
            }
            Boolean b= this.saveOrUpdateBatch(param.getInfoList());
            if (b) {
                result = Result.succ(1, "保存成功", null);
            } else {
                result = Result.fail(400, "保存失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "保存失败", "");
        }
        return result;
    }
}

